﻿namespace WMS6.Web.CrossDock
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CrossdockServiceRef;
    using WMS6.Web.UserControls;

    public class FlowThruOrderList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F79562;
        protected GridView gvXorderQuery;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblArea;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2XordersList(XordersQueryEntity xOrderQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(xOrderQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = CrossdockServiceClientProxy.GetInstance().GetXordersInfoByQueryEntity(xOrderQuery);
            base.BindData2GridView(this.gvXorderQuery, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvXorderQuery, "ORDER_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = "请选择一项!";
                }
                else
                {
                    string strTemp = CrossdockServiceClientProxy.GetInstance().DeleteXordersInfo(selectedID);
                    this.lblMsg.Text = strTemp + "删除成功!";
                    XordersQueryEntity xOrderQuery = this.GetXorderQueryEntity();
                    this.BindData2XordersList(xOrderQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "删除失败:" + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvXorderQuery, "ORDER_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请选择一项! ";
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = "只能选择一项! ";
            }
            else
            {
                base.Response.Redirect(string.Format("FlowThruOrder.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            XordersQueryEntity xOrderQuery = this.GetXorderQueryEntity();
            xOrderQuery.CurrentPage = "0";
            this.BindData2XordersList(xOrderQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            XordersQueryEntity xOrderQuery = this.GetXorderQueryEntity();
            xOrderQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2XordersList(xOrderQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            XordersQueryEntity xOrderQuery = this.GetXorderQueryEntity();
            if (int.Parse(xOrderQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                xOrderQuery.CurrentPage = (int.Parse(xOrderQuery.CurrentPage) + 1).ToString();
                this.BindData2XordersList(xOrderQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            XordersQueryEntity xOrderQuery = this.GetXorderQueryEntity();
            if (int.Parse(xOrderQuery.CurrentPage) > 0)
            {
                xOrderQuery.CurrentPage = (int.Parse(xOrderQuery.CurrentPage) - 1).ToString();
                this.BindData2XordersList(xOrderQuery);
            }
        }

        private XordersQueryEntity GetXorderQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as XordersQueryEntity);
            }
            return (base.PageQueryEntitySession as XordersQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                XordersQueryEntity xOrderQuery = this.GetXorderQueryEntity();
                xOrderQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2XordersList(xOrderQuery);
            }
        }

        protected void gvXorderQuery_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2132", this.gvXorderQuery);
        }

        protected void gvXorderQuery_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvXorderQuery_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                XordersQueryEntity xOrderQuery = this.GetXorderQueryEntity();
                xOrderQuery.SortField = e.SortExpression;
                if (xOrderQuery.SortDirection == "ASC")
                {
                    xOrderQuery.SortDirection = "DESC";
                }
                else if (xOrderQuery.SortDirection == "DESC")
                {
                    xOrderQuery.SortDirection = "ASC";
                }
                this.BindData2XordersList(xOrderQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            XordersQueryEntity xOrderQuery = new XordersQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    xOrderQuery.OrderID = this.PreviousPage.OrderID;
                    xOrderQuery.OwnerID = this.PreviousPage.OwnerID;
                    xOrderQuery.PoID = this.PreviousPage.PoID;
                }
                else
                {
                    xOrderQuery.OrderID = "";
                    xOrderQuery.OwnerID = "";
                    xOrderQuery.PoID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            xOrderQuery.IsGetAll = false;
            xOrderQuery.CurrentPage = "0";
            xOrderQuery.PageSize = this.gvXorderQuery.PageSize.ToString();
            xOrderQuery.SortField = "ORDER_ID";
            xOrderQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvXorderQuery.PageSize;
            this.SetXorderQueryEntity(xOrderQuery);
            this.BindData2XordersList(xOrderQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetXorderQueryEntity(XordersQueryEntity xOrderQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = xOrderQuery;
            }
            else
            {
                base.PageQueryEntitySession = xOrderQuery;
            }
        }

        public FlowThruOrderQuery PreviousPage
        {
            get
            {
                return (FlowThruOrderQuery) base.PreviousPage;
            }
        }
    }
}

